#!/usr/bin/perl

use strict;
use POSIX;

# arguments are RA, Dec, start LST
my ($ra,$dec,$start_lst)=@ARGV;

my $angle_inc=22.5; # in degrees

my $name_prefix="4bm";

my $curr_lst=$start_lst;
my $spacing=0.013;
my $n_points=33;
my $n_cycles=3;
for (my $curr_angle=0.0;$curr_angle<360.0;$curr_angle+=$angle_inc){
    my $this_name=$name_prefix.floor($curr_angle);
    print "make mosaic: $this_name\n";
    
    my $command="./altaz_mosaic.pl ".$ra." ".$dec." ".$curr_lst." ".
	$curr_angle." ".$spacing." ".$n_points." ".$n_cycles." ".
	$this_name;
    print " executing command: $command\n";
    open(INPUT,"-|")||exec $command;
    my $new_lst;
    while(<INPUT>){
	chomp;
	my $line=$_;
	if ($line=~/\snew\sLST/){
	    $new_lst=$line;
	    $new_lst=~s/.*\=\s(.*)/$1/;
	}
    }
    close(INPUT);
    
    my @lst_els=split(/\:/,$new_lst);
    my $lst_decimal=($lst_els[0]+$lst_els[1]/60.0+$lst_els[2]/3600.0)*15.0;
    my $cycle_time=10.0*15.0/3600.0; # assume 10 seconds cycle time
    my $lst_decimal+=$cycle_time;
    my ($new_lst,$new_lst_hour,$new_lst_minute,$new_lst_second);
    $new_lst=$lst_decimal/15.0;
    $new_lst_hour=floor($new_lst);
    $new_lst-=$new_lst_hour;
    $new_lst*=60.0;
    $new_lst_minute=floor($new_lst);
    $new_lst-=$new_lst_minute;
    $new_lst*=60.0;
    $new_lst_second=floor($new_lst);

    $curr_lst=$new_lst_hour.":".$new_lst_minute.":".$new_lst_second;
}
